/**
 * ReportGear(2011)
 */
package com.reportgear.designer.action.support;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;

import com.reportgear.core.data.SalaryHelper;
import com.reportgear.core.data.model.SalaryDataObject;
import com.reportgear.core.swing.layout.LayoutHelper;
import com.reportgear.core.swing.pane.base.JBasePane;
import com.reportgear.report.model.cell.value.support.CommonObject;
import com.reportgear.swing.controls.JTreeComboBox;

/**
 * 新报表模板
 * 
 * @version 1.0 2011-4-17
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class JNewReportPane extends JBasePane {
	/**
	 * UID
	 */
	private static final long serialVersionUID = -2061060882705944858L;
	private JTextField txtName;
	private JTreeComboBox cbxScheme;

	public JNewReportPane() {
		this.initComponents();
	}

	// 输入是否有效
	public boolean isInputValid() {
		if ("".equals(this.getName()) || this.getSchemeId() == -1) {
			return false;
		} else {
			return true;
		}
	}

	public String getName() {
		return this.txtName.getText().trim();
	}

	public int getSchemeId() {
		DefaultMutableTreeNode selectNode = cbxScheme.getSelectedTreeNode();

		if (selectNode == null) {
			return -1;
		} else {
			return ((CommonObject) selectNode.getUserObject()).getId();
		}
	}

	private void initComponents() {
		this.setLayout(new GridBagLayout());

		this.add(new JLabel("报表名称", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 0, 1, 1));
		this.txtName = new JTextField();
		this.txtName.setPreferredSize(new Dimension(300, 24));
		this.add(this.txtName, LayoutHelper.newGridBagConstraints(1, 0, 1, 1));

		this.add(new JLabel("薪酬体系", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 1, 1, 1));
		this.cbxScheme = this.createScheme();
		this.add(this.cbxScheme, LayoutHelper.newGridBagConstraints(1, 1, 1, 1));
	}

	private JTreeComboBox createScheme() {
		JTreeComboBox cbx = new JTreeComboBox();

		List<CommonObject> items = this.getSalarySchemes();

		DefaultMutableTreeNode root = new DefaultMutableTreeNode("项目体系");
		DefaultMutableTreeNode pNode = null;

		for (CommonObject o : items) {
			MutableTreeNode node = new DefaultMutableTreeNode(o);
			if ((pNode != null) && ((CommonObject) pNode.getUserObject()).getId() == o.getParentId()) {
				pNode.add(node);
			} else {
				pNode = this.searchParentNode(root, o);
				if (pNode == null) {
					root.add(node);
				} else {
					pNode.add(node);
				}
			}
		}

		JTree tree = new JTree(root);
		cbx.setTree(tree);

		return cbx;
	}

	/**
	 * 获取薪酬体系
	 * 
	 * @return 薪酬体系
	 */
	private List<CommonObject> getSalarySchemes() {
		List<SalaryDataObject> sdoList = SalaryHelper.INSTANCE.getSalaryScheme();
		List<CommonObject> items = new ArrayList<CommonObject>();
		for (SalaryDataObject sdo : sdoList) {
			items.add(new CommonObject(sdo.getInt("id"), sdo.getInt("parentId"), sdo.getString("code"), sdo
					.getString("name")));
		}

		Collections.sort(items);

		return items;
	}

	@SuppressWarnings("unchecked")
	private DefaultMutableTreeNode searchParentNode(TreeNode root, CommonObject co) {
		int pid = co.getParentId();
		DefaultMutableTreeNode node = null;
		DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) root;

		Enumeration e = rootNode.breadthFirstEnumeration();
		while (e.hasMoreElements()) {
			node = (DefaultMutableTreeNode) e.nextElement();
			Object o = node.getUserObject();
			if (o instanceof CommonObject) {
				if (((CommonObject) o).getId() == pid) {
					return node;
				}
			}
		}

		return null;
	}
}
